﻿//用于列表页和详细页公用的权限部分
var ListAndDetailRight = {};
var ldr = ListAndDetailRight;
//转盘给个人权限
ldr.DTrunRight = "Property.TurnPrivate.Modify-All";
ldr.RTrunSelf = "Property.TurnPrivate.Modify-MySelf";
ldr.RTrunDepart = "Property.TurnPrivate.Modify-MyDepartment";
ldr.RTrunAll = "Property.TurnPrivate.Modify-All";
//转盘给公盘权限
ldr.DTrunPRight = "Property.TurnPublic.Modify-All";
ldr.RTrunPSelf = "Property.TurnPublic.Modify-MySelf";
ldr.RTrunPDepart = "Property.TurnPublic.Modify-MyDepartment";
ldr.RTrunPAll = "Property.TurnPublic.Modify-All";
//修改状态权限
ldr.DStatusRight = "Property.PropertyStatus.Modify-All";
ldr.RStatusSelf = "Property.PropertyStatus.Modify-MySelf";
ldr.RStatusDepart = "Property.PropertyStatus.Modify-MyDepartment";
ldr.RStatusAll = "Property.PropertyStatus.Modify-All";
//设置集中代看
ldr.RTogetherTakeSeeSelf = "Property.TogetherTakeSee.Set-MySelf";
ldr.RTogetherTakeSeeDepart = "Property.TogetherTakeSee.Set-MyDepartment";
ldr.RTogetherTakeSeeAll = "Property.TogetherTakeSee.Set-All";
//查看实勘权限
ldr.DSeeReal = "Property.RealSurvey.Search-All";
ldr.RSeeRealSelf = "Property.RealSurvey.Search-MySelf";
ldr.RSeeRealDepart = "Property.RealSurvey.Search-MyDepartment";
ldr.RSeeRealAll = "Property.RealSurvey.Search-All";

//新增实勘权限
ldr.DAddReal = "Property.RealSurvey.Add-All";
ldr.RAddRealSelf = "Property.RealSurvey.Add-MySelf";
ldr.RAddRealDepart = "Property.RealSurvey.Add-MyDepartment";
ldr.RAddRealAll = "Property.RealSurvey.Add-All";
//钥匙权限

//验证跟进删除权限
//propertys是房源集合,其中必须包含PropertyDepartmentKeyId,跟进房源做属部门
//DepartmentPermissions 操作人对跟进所属房源的部门权限
//keyid 跟进id
//estateName 房源楼盘
//userid 跟进填写人
//用于跟进左导页的跟进删除
ListAndDetailRight.CheckTrunRight = function (propertys) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    for (var i = 0; i < propertys.length; i++) {
        var item = $(propertys[i]);
        //判断操作人是否有删除所属房源的部门权限,比如A部门能不能删除房源B的跟进
        if (item.attr("right").indexOf(ldr.DTrunRight) == -1) {
            return false;
        }
        //如果有部门权限,那么判断角色权限
        //本人
        if (roleRight.indexOf(ldr.RTrunSelf) > -1) {
            if (item.attr("userid") != operator) {
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTrunDepart) > -1) {
            if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTrunAll) > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    return true;
}
ListAndDetailRight.CheckTrunPRight = function (propertys) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    for (var i = 0; i < propertys.length; i++) {
        var item = $(propertys[i]);
        //判断操作人是否有删除所属房源的部门权限,比如A部门能不能删除房源B的跟进
        if (item.attr("right").indexOf(ldr.DTrunPRight) == -1) {
            return false;
        }
        //如果有部门权限,那么判断角色权限
        //本人
        if (roleRight.indexOf(ldr.RTrunPSelf) > -1) {
            if (item.attr("userid") != operator) {
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTrunPDepart) > -1) {
            if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTrunPAll) > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    return true;
}
ListAndDetailRight.CheckStatusRight = function (propertys) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    for (var i = 0; i < propertys.length; i++) {
        var item = $(propertys[i]);
        //本人
        if (roleRight.indexOf(ldr.RStatusSelf) > -1) {
            if (item.attr("userid") != operator) {
                $.OKDialogAgency('<div class="tc">您拥有的是修改本人房源状态的权限,但该楼盘的所属人不是您！</div>');
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RStatusDepart) > -1) {
            if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
                $.OKDialogAgency('<div class="tc">您拥有的是修改本部房源状态的权限,但该房源不属您管辖部门！</div>');
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RStatusAll) > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    return true;
}
ListAndDetailRight.CheckTogetherTakeSeeRight = function (propertys) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    for (var i = 0; i < propertys.length; i++) {
        var item = $(propertys[i]);
        //如果有部门权限,那么判断角色权限
        //本人
        if (roleRight.indexOf(ldr.RTogetherTakeSeeSelf) > -1) {
            if (item.attr("userid") != operator) {
                $.OKDialogAgency('<div class="tc">您拥有的是集中带看本人房源状态的权限,但该楼盘的所属人不是您！</div>');
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTogetherTakeSeeDepart) > -1) {
            if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
                $.OKDialogAgency('<div class="tc">您拥有的是集中带看本部房源状态的权限,但该房源不属您管辖部门！</div>');
                return false;
            }
        }
        else if (roleRight.indexOf(ldr.RTogetherTakeSeeAll) > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    return true;
}

//部门角色都有权限判断
ListAndDetailRight.CheckRight = function (el, key, alertStr) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    var item = $(".btnValues[keyid=" + $(el).attr("keyid") + "]");
    //如果有部门权限,那么判断角色权限
    if (item.attr("right").indexOf(key + "-All") == -1) {
        if (alertStr) {
            $.OKDialogAgency('<div class="tc">你没有' + alertStr + '部门权限！</div>');
        }
        return false;
    }
    //本人
    if (roleRight.indexOf(key + "-MySelf") > -1) {
        if (item.attr("userid") != operator) {
            if (alertStr) {
                $.OKDialogAgency('<div class="tc">你的操作对象不属于本人！</div>');
            }
            return false;
        }
    }
    else if (roleRight.indexOf(key + "-MyDepartment") > -1) {
        if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
            if (alertStr) {
                $.OKDialogAgency('<div class="tc">你的操作对象不属于你所在部门！</div>');
            }
            return false;
        }
    }
    else if (roleRight.indexOf(key + "-All") > -1) {
        return true;
    }
    else {
        if (alertStr) {
            $.OKDialogAgency('<div class="tc">你没有' + alertStr + '的权限！</div>');
        }
        return false;
    }

    return true;
}
//判断角色权限
ListAndDetailRight.CheckRightRol = function (el, key, isAlert) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    var item = $(el);
    if (roleRight.indexOf(key + "-MySelf") > -1) {
        if (item.attr("userid") != operator) {
            if (isAlert) {
                $.OKDialogAgency('<div class="tc">您拥有的是本人权限,但该记录不属于你本人！</div>');
            }
            return false;
        }
    }
    else if (roleRight.indexOf(key + "-MyDepartment") > -1) {
        if (myDepartments.indexOf(item.attr("departmentid")) == -1) {
            if (isAlert) {
                $.OKDialogAgency('<div class="tc">您拥有的是本部权限,但该记录不属于你的部门！</div>');
            }
            return false;
        }
    }
    else if (roleRight.indexOf(key + "-All") > -1) {
        return true;
    }
    else {
        if (isAlert) {
            $.OKDialogAgency('<div class="tc">您没有对应的操作权限！</div>');
        }
        return false;
    }

    return true;
}
//查看联系人操作日志
ListAndDetailRight.CheckRightByContactInformation = function (el, key, alertStr) {
    var roleRight = $("#hidPermissions").val();
    var myDepartments = $("#hidMyDepartments").val();
    var operator = $("#hidOperatorID").val();
    var item = $(".btnValues[keyid=" + $(el).attr("keyid") + "]");
    //如果有部门权限,那么判断角色权限
    if (item.attr("right").indexOf(key + "-None") == -1) {
        if (alertStr) {
            $.OKDialogAgency('<div class="tc">你没有' + alertStr + '部门权限！</div>');
        }
        return false;
    }
    //本人
    if (roleRight.indexOf(key + "-IsDep") > -1) {
           return true;
    }
    else if (roleRight.indexOf(key + "-NoDep") > -1) {
        return true;
    }
    else {
        if (alertStr) {
            $.OKDialogAgency('<div class="tc">你没有' + alertStr + '的权限！</div>');
        }
        return false;
    }

    return true;
}
